
<template>
  <div>
    <Modal v-model="modal" width="900" :mask-closable="false" title="组织机构列表" :footer-hide="false">
      <Form :label-width="110" ref="formInline" :model="formInline">
        <Row :gutter="16" class="query-form">
          <i-col span="8">
            <FormItem label="公司名称：">
              <Input class="query-form-el" v-model="formInline.orgName"></Input>
            </FormItem>
          </i-col>
          <i-col span="8" offset="8">
            <div style="float: right;">
              <Button class="ivu-btn-reset" @click="resetOrg">重置</Button>
              <Button class="ivu-btn-query" icon="md-search" @click="queryOrg">查询</Button>
            </div>
          </i-col>
        </Row>
      </Form>
      <Table row-key='orgId' ref='table2' height="350" :loading="loading2" no-data-text="没有查询到相关数据!" border @on-select="selectionTable"
             :columns="orgColumn" :load-data="handleLoadData" width="100%" :data="orgDatas" size="small">
      </Table>
      <Form :label-width="100">
        <Row style="margin-top: 10px;">
          <i-col span="24">
            <FormItem label="已选择企业">
              <TagInput ref="refTagInput" :tags="selectOrg"/>
            </FormItem>
          </i-col>
        </Row>
      </Form>
      <div slot="footer">
        <Button class="ivu-btn-save" @click="saveInfo">确定</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import { sessionStorage } from '../common/cacheUtils.js';
import {Api} from '../api/index.js'
import TagInput from '@/components/cascader-dom-tag'
export default {
  name:"cascaderDomMultiple",
  components:{TagInput},
  props: {
    codeAffordIf:{
      type: String,
      default: '0'
    },
    multiple:{
      type: Boolean,
      default: false
    },
    rootOrgId:{
      type:String,
      default:'18000000'
    },
  },
  data() {
    return {
      modal: false,
      selectOrg:[],
      orgDatas: [],
      formInline: {},
      loading2: false,
      orgLevelList: [],
      orgColumn:[
        {
          title: '组织机构名称',
          minWidth: 200,
          align: 'left',
          key: 'orgName',
          tree:true
        },
        {
          title: '组织机构级别',
          minWidth: 150,
          align: 'center',
          key: 'orgLevel',
          render:(h, params) => {
            return h('span', this.orgLevelMap[params.row.orgLevel]);
          },
        },
        {
          title: '操作',
          align: 'center',
          key: 'view',
          fixed: 'right',
          minWidth: 80,
          render: (h, params) => {
            return h('div', [
              h('A', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                style: {
                  fontSize: '14px'
                },
                on: {
                  click: () => {
                      this.addOrg(params.row);
                  }
                }
              }, '选择')
            ])
          }
        }
      ],
      orgLevelMap:{},
    }
  },
  methods: {
    saveInfo(){
      if(this.selectOrg!=undefined&&this.selectOrg.length!=0){
        this.$emit('setData', this.selectOrg);
        this.modal = false;
      }else {
        this.$Message.error('请选择数据！');
      }
    },
    addOrg(row){
      let orgInfo = {orgName: row.orgName, orgId: row.orgId};
      let exist = false;
      let orgId = row.orgId;
      this.selectOrg.forEach(v => {
        if (orgId == v.orgId) {
          exist = true;
        }
      });
      if (!exist) {
        this.$nextTick(() => {
          this.selectOrg.push(orgInfo);
        })
      }
    },
    selectCodingSort(){
      this.$http.get(Api.queryCodeDetailBySortId +'/0200100005',{params:{}}).then((res) => {
        this.orgLevelList = res.data.data;
        this.orgLevelList.forEach(item=>{
          this.orgLevelMap[item.codingCodeId] = item.codingName;
        })
      });
    },
    setOrgName(orgName){
      this.orgName = orgName;
    },
    openOrgModal(data){
      this.modal = true;
      if(data){
        this.selectOrg = data;
      }else{
        this.selectOrg = [];
      }

      this.getOrgInfo();
    },
    getOrgInfo(){
      let orgId = this.rootOrgId;
      if(orgId == null){
        orgId =  sessionStorage('userId').getObject('userId').companyId
      }
      this.$http.get(Api.queryOrgDetailById + "/" + orgId).then(res => {
        this.orgDatas = [];
        let temp = {};
        temp._loading = false;
        temp.children = [];
        temp.orgName = res.data.data.orgName;
        temp.orgLevel = res.data.data.orgLevel;
        temp.orgType = res.data.data.orgType;
        temp.orgId = res.data.data.orgId;
        this.orgDatas.push(temp);
      }).catch(e => {
        this.$Message.error('请求失败');
      })
    },
    handleLoadData (item, callback) {
      setTimeout(() => {
        this.$http.get(Api.queryOrg, {
          params: {
            parentId: item.orgId,
            codeAffordIf : 0
          }
        }).then(res => {
          if(res.data.data.length !== 0) {
            res.data.data.map(i => {
              if(i.orgLevel != '0200100005000000007'){
                i._loading = false;
                i.children = [];
              }
            })
            item.children = res.data.data;
            callback(item.children);
          } else {
            delete item['_loading'];
            item.children=[];
            callback([]);
          }
        }).catch(e => {
          this.$Message.error('请求失败');
        })
      }, 2000);
    },
    selectionTable(selection, row){
      this.modal = false;
      this.orgName = row.orgName;
      let data = [row.orgId];
      this.$emit('setOrgInfo', data);
      this.$emit('setOrgname', row.orgName);
    },
    resetOrg(){
      this.formInline.orgName = '';
      this.formInline.orgLevel = '';
      this.getOrgInfo();
    },
    queryOrg(){
      this.loading2 = true;
      this.$http.get(Api.queryOrgList+"?orgName="+this.formInline.orgName+"&fromPage=1&pageSize=1000").then(res => {
        let datas = [];
        res.data.data.map(i => {
        //  if(i.orgLevel == '13' || i.orgLevel == '12'){
        //    return;
       //  }
          if(i.orgLevel != '0200100005000000007'){
            i._loading = false;
            i.children = [];
          }
          datas.push(i);
        })
        this.orgDatas = datas;
        this.loading2 = false;
      }).catch(e => {
        this.$Message.error('请求失败');
        this.loading2 = false;
      })
    },
    resetCurrentOrg(){
      this.formInline.orgName = '';
      this.formInline.orgLevel = '';
      this.orgName = '';
    },
    cancel(){
      this.formInline= {
        orgName: ''
      };
      this.queryOrg();
    }
  },
  computed: {
  },
  mounted() {

    this.isModal = false;
  }
}
</script>

<style lang="less">
/*修改分页样式*/

.Cascader {
  display: block;
}
</style>
